﻿#pragma once


namespace bite
{

    
    // vector和list反向迭代器实现
    // 
    // 
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T())
            :_val(val)
            ,_pPre(nullptr)
            ,_pNext(nullptr)
        {}
        ListNode<T>* _pPre;
        ListNode<T>* _pNext;
        T _val;

    };


    //List的迭代器类
    template<class T, class Ref, class Ptr>
    struct ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;
       
        PNode _pNode;
        ListIterator(PNode pNode = nullptr)            :_pNode(pNode)
        {}

        ListIterator(const Self& l)
            :_pNode(l._pNode)
        {}

        Ref operator*()
        {
            return _pNode->_val;
        }
        Ptr operator->()
        {
            return &_pNode->_val;
        }
        Self& operator++()
        {
            _pNode = _pNode->_pNext;
            return *this;
        }
        Self operator++(int)
        {
            Self op(*this);
            _pNode = _pNode->_pNext;
            return op;
        }
        Self& operator--()
        {
            _pNode = _pNode->_pPre;
            return *this;
        }
        Self operator--(int)
        {
            Self op(*this);
            _pNode = _pNode->_pPre;
            return op;
        }
        bool operator!=(const Self& l)
        {
            return _pNode != l._pNode;
        }
        bool operator==(const Self& l)
        {
            return _pNode == l._pNode;

        }

      
    };


    

    //list类
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;


    public:
        typedef ListIterator<T, T&, T*> iterator;

        typedef ListIterator<T, const T&, const T&> const_iterator;


      

    public:
        ///////////////////////////////////////////////////////////////
        // List的构造
        list()
        {
            CreateHead();
        }
        list(int n, const T& value = T())
        {
            CreateHead();
            for (int i = 0; i < n; i++)
            {
                push_back(value);
            }
        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            CreateHead();
            while (first != last)
            {
                push_back(*first);
                ++first;
            }
        }

        //s2(s1)
        list(const list<T>& l)
        {
            CreateHead();
            for (auto node : l)
            {
                push_back(node);
            }
        }

        list<T>& operator=(list<T> l)
        {
            swap(l);
            return *this;
        }
        ~list()
        {
            clear();
            delete _pHead;
            _pHead = nullptr;
            _size = 0;
        }


        ///////////////////////////////////////////////////////////////
        // List Iterator
        iterator begin()
        {
            return iterator(_pHead->_pNext);
        }


        iterator end()
        {
            return iterator(_pHead);
        }

        const_iterator begin()const
        {
            return const_iterator(_pHead->_pNext);
        }

        const_iterator end()const
        {
            return const_iterator(_pHead);
        }


        ///////////////////////////////////////////////////////////////
        // List Capacity
        size_t size()const
        {
            return _size;
        }
        bool empty()const
        {
            return _size == 0;
        }

        ////////////////////////////////////////////////////////////
        // List Access
        T& front()
        {
            return _pHead->_pNext->_val;
        }
        const T& front()const
        {
            return _pHead->_pNext->_val;
        }

        T& back()
        {
            return _pHead->_pPre->_val;
        }

        const T& back()const
        {
            return _pHead->_pPre->_val;
        }


        ////////////////////////////////////////////////////////////
        // List Modify
        void push_back(const T& val) 
        { 
            insert(iterator(end()), val); 
        }
        void pop_back() 
        {
            erase(--end());
        }
        void push_front(const T& val) 
        { 
            insert(begin(), val); 
        }
        void pop_front() 
        { 
            erase(begin());
        }
        // 在pos位置前插入值为val的节点
        iterator insert(iterator pos, const T& val)
        {
            PNode newnode = new Node(val);
            PNode cur = pos._pNode;
            PNode Pre = cur->_pPre;
           

            Pre->_pNext = newnode;
            newnode->_pPre = Pre;

            newnode->_pNext = cur;
            cur->_pPre = newnode;
            ++_size;
            return newnode;
        }
        // 删除pos位置的节点，返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            PNode cur = pos._pNode;
            PNode pre = cur->_pPre;
            PNode next = cur->_pNext;

            delete cur;

            pre->_pNext = next;
            next->_pPre = pre;

            --_size;
            return  iterator(next);
        }
        void clear()
        {
            /*for (auto node : *this)
            {
                erase(node);
            }*/
            iterator it = begin();
            while (it != end()--)
            {
                it = erase(it);
            }
        }

        void swap(list<T>& lt)
        {
            std::swap(_pHead, lt._pHead);
            std::swap(_size, lt._size);
        }
    private:
        void CreateHead()
        {
            _pHead = new Node;
            _size = 0;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        PNode _pHead;
        size_t _size;
    };
};


void Test_list1()
{
    bite::list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    lt1.push_back(5);

   /* bite::list<int>::iterator it = lt1.begin();
    while (it != lt1.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;*/

    for (auto num : lt1)
    {
        cout << num << " ";
    }
    cout << endl;

    lt1.erase(lt1.begin());

    for (auto num : lt1)
    {
        cout << num << " ";
    }
    cout << endl;


    lt1.erase(--lt1.end());

    for (auto num : lt1)
    {
        cout << num << " ";
    }
    cout << endl;


    lt1.clear();
    for (auto num : lt1)
    {
        cout << num << " ";
    }
    cout << endl;

}




void Test_list2()
{
    bite::list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    lt1.push_back(5);

   /* bite::list<int> lt2(10,2);
    for (auto num : lt2)
    {
        cout << num << " ";
    }
    cout << endl;*/

    //bite::list<int> lt2(lt1);
    //for (auto num : lt2)
    //{
    //    cout << num << " ";
    //}
    //cout << endl;

    bite::list<int> lt3;
    lt3 = lt1;
    for (auto num : lt3)
    {
        cout << num << " ";
    }
    cout << endl;

    /*bite::list<int> lt3(lt1.begin(),lt1.begin()++);
    for (auto num : lt3)
    {
        cout << num << " ";
    }
    cout << endl;*/

}




void Test_list3()
{
    bite::list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    lt1.push_back(5);
  /*  for (auto num : lt1)
    {
        cout << num << " ";
    }
    cout << endl;*/

    cout << lt1.front() << endl;
    cout << lt1.back() << endl;

    bite::list<int>  lt3(lt1.begin(), lt1.end());
    for (auto num : lt3)
    {
        cout << num << " ";
    }
    cout << endl;


   /* const bite:: list<int> lt2;
    lt2.push_back(1);
    lt2.push_back(2);
    lt2.push_back(3);
    lt2.push_back(4);
    lt2.push_back(5);*/

    /*cout << lt2.front() << endl;
    cout << lt2.back() << endl;*/
}





void Test_list4()
{
    bite::list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    lt1.push_back(5);

    

}